﻿using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Abp.Organizations;
using BXJG.Utils.Application.Share.GeneralTree;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZLJ.Application.Common.Share.OU;
using ZLJ.Core.OU;
using ZLJ.Core.Share;

namespace ZLJ.Application.Common.OU
{
    /// <summary>
    /// 提供公司、部门下拉树形数据；登陆用户即可访问，将来增加权限依赖
    /// </summary>
    [AbpAuthorize]
    [UnitOfWork(false)]
    public class OUSelectProviderAppService : CommonBaseAppService, IOUSelectProviderAppService
    {
        IRepository<OrganizationUnit, long> repository;

        public OUSelectProviderAppService(IRepository<OrganizationUnit, long> repository)
        {
            this.repository = repository;
        }
        /// <summary>
        /// 提供公司、部门下拉树形数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task<IList<OUSelectDto>> GetTreeForSelectAsync(GetListInput input)
        {
            if (input.ParentCode.IsNullOrWhiteSpace() && input.ParentId.HasValue && input.ParentId.Value > 0)
                input.ParentCode = await repository.GetAll().Where(c => c.Id == input.ParentId).Select(c => c.Code).SingleAsync();

            var q = repository.GetAll();

            if (input.WhatType == 0)
                q = q.OfType<OrganizationUnitEntity>();
            else
                q = q.OfType<ZLJ.Core.Customer.CustomerOUEntity>();
            q = q.AsNoTrackingWithIdentityResolution();

            if (input.IsOnlyLoadChild)
            {
                q = q.WhereIf(input.ParentCode.IsNotNullOrWhiteSpaceBXJG(), c => c.Parent.Code == input.ParentCode)
                     .WhereIf(input.ParentCode.IsNullOrWhiteSpaceBXJG(), c => !c.ParentId.HasValue);
            }
            else
            {
                q = q.Include(c => c.Children).WhereIf(input.ParentCode.IsNotNullOrWhiteSpaceBXJG(), c => c.Code != input.ParentCode && c.Code.StartsWith(input.ParentCode));
            }

            q = q.OrderBy(c => c.Code);
            var list = await q.ToListAsync();
            var dtos = new List<OUSelectDto>();
            foreach (var item in list)
            {
                var item2 = item as OrganizationUnitEntity;
                dtos.Add(new OUSelectDto
                {
                    Checked = default,
                    Code = item.Code,
                    IconCls = "ou",
                    Id = item.Id,
                    Text = item.DisplayName,
                    //State = item.Children.Count>0"opend":"closed"
                    ParentId = item.ParentId,
                    OUType = item2 == default ? OUType.HeadOffice : item2.OUType,
                    DisplayName = item.DisplayName,
                });
            }
            if (!input.IsOnlyLoadChild)
            {
                dtos.ForEach(c =>
                {
                    c.Children = dtos.Where(d => d.ParentId == c.Id).ToList();
                    c.ChildrenCount = c.Children.Count;
                });
                if (input.ParentCode.IsNullOrEmpty())
                    return dtos.Where(c => !c.ParentId.HasValue).ToList();
                return dtos.Where(c => c.Parent.Code == input.ParentCode).ToList();
            }
            else
            {
                return dtos;
            }
        }

        public Task<IList<GeneralTreeComboboxDto>> GetNodesForSelectAsync(GeneralTreeGetForSelectInput input)
        {
            throw new NotImplementedException();
        }
    }
}
